<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--店员数据-->
      <el-col :span="24" :xs="24">
        <!-- 搜索条件 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="店员名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入店员名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="店员昵称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入店员昵称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="店铺ID" prop="shopId" v-if="$store.getters && $store.getters.admin">
            <el-input
              v-model="queryParams.shopId"
              placeholder="请输入店铺ID"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 操作按钮组 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['employee:static:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['employee:static:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <template>
          <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <el-table-column label="店员" align="center" key="userId" prop="userId" :show-overflow-tooltip="true" min-width="180" >
              <template slot-scope="scope">
                <div class="flex-align-center" v-if="scope.row.employeeUser">
                  <el-avatar shape="circle" style="width: 40px" size="medium" :src="scope.row.employeeUser.avatar" alt="404"></el-avatar>
                  <div class="mgl5 font-14">{{scope.row.employeeUser.nickName}}</div>
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column label="余额" align="center" key="balance" prop="balance" sortable="custom" :sort-orders="['descending', 'ascending']" width="100"/>
            <el-table-column label="今日佣金" align="center" key="moneyToday" prop="moneyToday" sortable="custom" :sort-orders="['descending', 'ascending']" width="120"/>
            <el-table-column label="今日被打赏礼物数" align="center" key="rewardToday" prop="rewardToday" sortable="custom" :sort-orders="['descending', 'ascending']" width="150"/>
            <el-table-column label="今日被打赏金额" align="center" key="rewardAmountToday" prop="rewardAmountToday" sortable="custom" :sort-orders="['descending', 'ascending']" width="150"/>
            <el-table-column label="今日订单额" align="center" key="orderAmountToday" prop="orderAmountToday" sortable="custom" :sort-orders="['descending', 'ascending']" width="150"/>
            <el-table-column label="今日接单数" align="center" key="orderToday" prop="orderToday" sortable="custom" :sort-orders="['descending', 'ascending']" width="120"/>

            <el-table-column label="已结算总金额" align="center" key="moneyGet" prop="moneyGet" sortable="custom" :sort-orders="['descending', 'ascending']" width="130"/>
            <el-table-column label="被打赏礼物总数" align="center" key="rewardCount" prop="rewardCount" sortable="custom" :sort-orders="['descending', 'ascending']" width="140"/>
            <el-table-column label="被打赏总金额" align="center" key="rewardAmount" prop="rewardAmount" sortable="custom" :sort-orders="['descending', 'ascending']" width="130"/>
            <el-table-column label="订单总额" align="center" key="orderAmount" prop="orderAmount" sortable="custom" :sort-orders="['descending', 'ascending']" width="120"/>
            <el-table-column label="接单总数" align="center" key="orderCount" prop="orderCount" sortable="custom" :sort-orders="['descending', 'ascending']" width="120"/>

            <el-table-column label="订单完成总数" align="center" key="orderSuccessCount" prop="orderSuccessCount" sortable="custom" :sort-orders="['descending', 'ascending']" width="130"/>
            <el-table-column label="超时未接单总数" align="center" key="orderOvertimeCount" prop="orderOvertimeCount" sortable="custom" :sort-orders="['descending', 'ascending']" width="150"/>

            <el-table-column label="佣金比例" align="center" key="ratio" prop="ratio" sortable="custom" :sort-orders="['descending', 'ascending']" width="100"/>
            <el-table-column label="被投诉总数" align="center" key="reportCount" prop="reportCount" sortable="custom" :sort-orders="['descending', 'ascending']" width="120" />
            <el-table-column label="店铺名称" prop="shopInfo.shopName" v-if="$store.getters && $store.getters.admin" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="160" sortable="custom" :sort-orders="['descending', 'ascending']">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="更新时间" align="center" prop="updateTime" width="160" sortable="custom" :sort-orders="['descending', 'ascending']">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.updateTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              fixed="right"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['employee:static:edit']"
                >修改</el-button>
                <!-- <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['employee:static:remove']"
                >删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
          
          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </el-col>
    </el-row>

    <!-- 添加或修改店员配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="分佣比例" prop="ratio">
              <el-input v-model="form.ratio" placeholder="请输入分佣比例" type="number" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listUser, getUser, updateUser } from "@/api/employee/static";

export default {
  name: "EmployeeStatic",
  dicts: [],
  components: { },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店员表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopId: undefined,
        nickName: undefined,
        userName: undefined,
        phonenumber: undefined,
      },
      // 表单校验
      rules: {
      },
    };
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店员列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        ratio: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店员";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))
          if (this.form.userId != undefined) {
            updateUser(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } 
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employee/static/export', {
        ...this.queryParams
      }, `店员统计_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>